﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonLibrary;
using CommonLibrary.BLL;
using CommonLibrary.Model;
using Microsoft.Win32;
using Yipusoft.SystemSet;

namespace SmsSeries
{
    public partial class frmSetup : Form
    {
        #region 属性

        /// <summary>
        /// 是否已经登录
        /// </summary>
        public bool HasLogin { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserID { get; set; }

        #endregion

        #region 控件事件

        //构造函数
        public frmSetup()
        {
            InitializeComponent();
        }

        //登录
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Login())
            {
                HasLogin = true;
                Hide();
            }
            else
            {
                HasLogin = false;
            }
        }

        //退出
        private void btnCancel_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        //窗体加载
        private void frmSetup_Load(object sender, EventArgs e)
        {
            Init();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 登录
        /// </summary>
        private bool Login()
        {
            List<UserInfoInfo> userInfoInfos = new List<UserInfoInfo>();

            userInfoInfos = UserInfo.GetList("");

            UserInfoInfo userInfoInfo =
                    userInfoInfos.Where(o => o.UserName.ToString() == tbUserName.Text.Trim()).FirstOrDefault();


            if (userInfoInfo != null)
            {
                if (userInfoInfo.StartTime <= DateTime.Now && userInfoInfo.EndTime >= DateTime.Now)
                {
                    if (!userInfoInfo.Locked)
                    {
                        //n = 1;
                    }
                    else
                    {
                        //n = (int)Data.StateEnum.UserIsLocked;
                        MessageBox.Show("用户被锁定！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return false;
                    }
                }
                else
                {
                    //n = (int)Data.StateEnum.UserExpired;
                }
            }
            else
            {
                //n = (int)Data.StateEnum.UserNotExist;
                MessageBox.Show("用户不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (userInfoInfo.Password != tbPassword.Text)
            {
                MessageBox.Show("密码不正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else
            {
                //登录成功
                UserID = userInfoInfo.UserID;
                return true;
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {


            //tbSQLServerName.Text = 

            //tbSQLPassword.Text = 
        }

        #endregion

        private void tbUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSubmit_Click(sender, e);
            }
        }

        private void tbPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSubmit_Click(sender, e);
            }
        }
    }
}
